package cn.supermaster.management.web.rest.system;

import cn.supermaster.management.common.annotation.Log;
import cn.supermaster.management.common.domain.SystemRole;
import cn.supermaster.management.common.domain.SystemUser;
import cn.supermaster.management.common.enums.BusinessType;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.system.domain.SystemUserRole;
import cn.supermaster.management.web.service.system.ISRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.rest.system
 * @ClassName: SystemRoleRestController
 * @Description: [角色信息RestController]
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/11 23:12
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@RestController
@RequestMapping("/system/role")
public class SystemRoleRestController {

    @Autowired
    private ISRoleService roleService;

    @RequiresPermissions("system:role:list")
    @PostMapping("/list")
    public TableDataInfo queryRoleListByCondition(SystemRole role) {
        return roleService.queryRoleListByCondition(role);
    }

    @Log(title = "角色管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:role:export")
    @PostMapping("/export")
    public WebResponse doExportRoleInformation(SystemRole role) {
        return roleService.doExportRoleInformation(role);
    }

    /**
     * 新增保存角色
     */
    @RequiresPermissions("system:role:add")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public WebResponse doAddRoleInformation(@Validated SystemRole role) {
        return roleService.doAddRoleInformation(role);
    }

    /**
     * 修改保存角色
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public WebResponse doModifyRoleInformation(@Validated SystemRole role) {
        return roleService.doModifyRoleInformation(role);
    }

    /**
     * 保存角色分配数据权限
     */
    @RequiresPermissions("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping("/authDataScope")
    public WebResponse doAddAuthDataScope(SystemRole role) {
        return roleService.doAddAuthDataScope(role);
    }

    @RequiresPermissions("system:role:remove")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public WebResponse doRemoveRoleInformation(String ids) {
        return roleService.doRemoveRoleInformation(ids);
    }

    /**
     * 校验角色名称
     */
    @PostMapping("/checkRoleNameUnique")
    public String doCheckRoleNameUnique(SystemRole role) {
        return roleService.doCheckRoleNameUnique(role);
    }

    /**
     * 校验角色权限
     */
    @PostMapping("/checkRoleKeyUnique")
    public String doCheckRoleKeyUnique(SystemRole role) {
        return roleService.doCheckRoleKeyUnique(role);
    }


    /**
     * 角色状态修改
     */
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:role:edit")
    @PostMapping("/changeStatus")
    public WebResponse doModifyRoleStatus(SystemRole role) {
        return roleService.doModifyRoleStatus(role);
    }


    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/authUser/allocatedList")
    public TableDataInfo queryAllocatedUserRoleList(SystemUser user) {
        return roleService.queryAllocatedUserRoleList(user);
    }

    /**
     * 取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancel")
    public WebResponse doCancelUserAuthorization(SystemUserRole userRole) {
        return roleService.doCancelUserAuthorization(userRole);
    }

    /**
     * 批量取消授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/cancelAll")
    public WebResponse doCancelAllUserAuthorizations(Long roleId, String userIds) {
        return roleService.doCancelAllUserAuthorizations(roleId, userIds);
    }

    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/authUser/unallocatedList")
    public TableDataInfo queryUnallocatedUserRoleList(SystemUser user) {
        return roleService.queryUnallocatedUserRoleList(user);
    }

    /**
     * 批量选择用户授权
     */
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PostMapping("/authUser/selectAll")
    public WebResponse queryUserAllAuthorizations(Long roleId, String userIds) {
        return roleService.queryUserAllAuthorizations(roleId, userIds);
    }
}